package org.nnxy.foodboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.nnxy.foodboot.entity.UserHealth;
import org.nnxy.foodboot.mapper.UserHealthMapper;
import org.nnxy.foodboot.service.UserHealthService;
import org.springframework.stereotype.Service;

/**
 * 用户健康Service实现类
 */
@Service
@RequiredArgsConstructor
public class UserHealthServiceImpl extends ServiceImpl<UserHealthMapper, UserHealth> implements UserHealthService {
    
    @Override
    public UserHealth getUserHealthByUserId(Integer userId) {
        if (userId == null) {
            return null;
        }
        
        LambdaQueryWrapper<UserHealth> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserHealth::getUserId, userId);
        
        return getOne(queryWrapper);
    }
    
    @Override
    public boolean saveOrUpdateUserHealth(UserHealth userHealth) {
        if (userHealth == null || userHealth.getUserId() == null) {
            return false;
        }
        
        UserHealth existingHealth = getUserHealthByUserId(userHealth.getUserId());
        
        if (existingHealth != null) {
            userHealth.setId(existingHealth.getId());
            return updateById(userHealth);
        } else {
            return save(userHealth);
        }
    }
} 